<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\BookOrder;
use App\Models\Child;
use App\Models\User;
use App\Services\CheckOrderService;
use App\Services\FaceService;
use Illuminate\Http\Request;

/**
 * Class FaceController
 * @package App\Http\Controllers\Api
 */
class FaceController extends Controller
{
    public function index(Request $request)
    {
        $img = $request->input("SceneData");
        $service = new FaceService();
        $user = $service->identifyPerson($img);
        if ($user) {
            if ($user instanceof User) {
                $order = (new CheckOrderService())->getOrder($user);
                if (!$order) {
                    return [
                        "RequestId"=> intval($request->input("RequestId")),
                        "Result"=> 0,
                        'Msg' => $user->name . '没有订餐'
                    ];
                } else {
                    $template = "<xffont color=\"#00FF00\" size=\"40px\">" . $user->name . ": 已订" . $order[0]->rule->type_label
                        . ", 已加" . $order->where('type', BookOrder::TYPE_ADDITION)->count() ."餐</xffont>";
                }
                $data = [
                    "RequestId"=> intval($request->input("RequestId")),
                    "Result"=> 1,
                    "Left"=>0,
                    "Top"=>0,
                    "DetailMsg"=>$template,
                    "UserInfo"=> [
                        "UserNo"=>(string)$user->id,
                        "Name"=>$user->name,
                        "UserIdentity"=>"tttt",
                        "Nation"=>"CN",
                        "Sex"=> 0,
                        "Birthday"=>"1990-12-12",
                        "ValidDate"=> date("Y.m.d") . "-" .date("Y.m.d")  ,
                        "Addr"=>'深圳',
                        "Department"=>"深圳",
                        "RoleType"=>$user->role->name,
                        "UserValidDate"=>date("Y.m.d") . "-" .date("Y.m.d")  ,
                        "CardNo"=> $user->id
                    ]
                ];
                return $data;
            }
            if ($user instanceof Child) {
                $order = (new CheckOrderService())->getOrderByChild($user);
                if (!$order) {
                    return [
                        "RequestId"=> intval($request->input("RequestId")),
                        "Result"=> 0,
                        'Msg' => $user->name . '没有订餐'
                    ];
                } else {
                    $template = "<xffont color=\"#00FF00\" size=\"40px\">" . $user->name . ":已订" . $order[0]->rule->type_label . "</xffont>";
                }
                $data = [
                    "RequestId"=> intval($request->input("RequestId")),
                    "Result"=> 1,
                    "Left"=>0,
                    "Top"=>0,
                    "DetailMsg"=>$template,
                    "UserInfo"=> [
                        "UserNo"=>(string)$user->id,
                        "Name"=>$user->name,
                        "UserIdentity"=>"tttt",
                        "Nation"=>"CN",
                        "Sex"=> 0,
                        "Birthday"=>"1990-12-12",
                        "ValidDate"=> date("Y.m.d") . "-" .date("Y.m.d")  ,
                        "Addr"=>'深圳',
                        "Department"=>"深圳",
                        "RoleType"=>"",
                        "UserValidDate"=>date("Y.m.d") . "-" .date("Y.m.d")  ,
                        "CardNo"=> $user->id
                    ]
                ];
                return $data;
            }
        } else {
            return "";
        }
    }
}
